class_name Move
extends NodeState

@export var player: Player
@export var animated_sprite_2d: AnimatedSprite2D
@export var speed: int = 200
@export var ray:RayCast2D

func _on_process(_delta : float) -> void:
	pass


func _on_physics_process(_delta : float) -> void:
	#print_debug("move:_on_physics_process")
	var direction: Vector2 = GameInputEvents.movement_input()
	
	if direction == Vector2.UP:
		animated_sprite_2d.play("move_up")
	elif direction == Vector2.RIGHT:
		animated_sprite_2d.play("move_right")
	elif direction == Vector2.DOWN:
		animated_sprite_2d.play("move_down")
	elif direction == Vector2.LEFT:
		animated_sprite_2d.play("move_left")
	
	if direction != Vector2.ZERO && direction !=player.player_direction:
		player.player_direction = direction
		# 将方向向量转换为角度偏移
		ray.target_position  = direction * GridUtils.CELL_SIZE  # 射线长度50像素
	player.velocity = direction * speed
	player.move_and_slide()

func _on_next_transitions() -> void:
	if !GameInputEvents.is_movement_input():
		transition.emit("Idle")


func _on_enter() -> void:
	pass


func _on_exit() -> void:
	animated_sprite_2d.stop()
